package run

import (
	"fmt"
	"os"
	"strings"

	"monkey/evaluator"
	"monkey/lexer"
	"monkey/object"
	"monkey/parser"
)

func Start(input string) {
	l := lexer.New(input)
	p := parser.New(l)
	program := p.ParseProgram()

	errors := p.Errors()
	if len(errors) != 0 {
		fmt.Println("Parser Exits with Errors:")
		fmt.Println(strings.Join(errors, "\n"))
		os.Exit(1)
	}

	env := object.NewEnvironment()
	macroEnv := object.NewEnvironment()

	evaluator.DefineMacros(program, macroEnv)
	expanded := evaluator.ExpandMacros(program, macroEnv)

	evaluated := evaluator.Eval(expanded, env)

	fmt.Println("Program Exits with Value: " + evaluated.Inspect())

}
